package com.freesky.provider.order.util;


import com.freesky.common.dao.entity.Thanks;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Freesky
 */
@Component
@Slf4j
public class EmailUtil {
    @Autowired
    private JavaMailSender sender;
    //邮件发送者
    @Value("${spring.mail.username}")
    private String mailUserName;
    @Value("${portal-server-url}")
    private String portalServerUrl;

    @Async
    public void sendEmailDealThank(String sendTo, String title, String tokenName, String token, Thanks thanks) {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailUserName);
            helper.setTo(sendTo);
            helper.setSubject("HTML邮件");
            // true表示HTML格式的邮件
            String content = "尊敬的管理员Freesky您好，您收到了来自" + title + "确认订单，请您及时处理：<br>";
            content += "-------------------------------------<br>";
            content += "登录账号：" + thanks.getUsername() + "<br>";
            content += "用户昵称：" + thanks.getNickName() + "<br>";
            content += "捐赠金额：" + thanks.getMoney() + "<br>";
            content += "支付方式：" + thanks.getPayType() + "<br>";
            content += "留言：" + thanks.getInfo() + "<br>";
            content += "通知邮箱：" + thanks.getEmail() + "<br>";
            content += "订单ID：" + thanks.getOrderId() + "<br>";
            content += "支付时间：" + thanks.getTime() + "<br>";
            content += "-------------------------------------<br>";
            content += "请点击以下相应链接完成对应处理操作：<br><br>";

            String passUrl = portalServerUrl + "/pay/pass?tokenName=" + tokenName + "&token=" + token + "&id=" + thanks.getId();
            content += "审核通过并展示捐赠数据：<br><a href='" + passUrl + "'>" + passUrl + "</a><br><br>";

            String backUrl = portalServerUrl + "/pay/back?tokenName=" + tokenName + "&token=" + token + "&id=" + thanks.getId();
            content += "审核驳回：<br><a href='" + backUrl + "'>" + backUrl + "</a><br><br>";

            String notShowUrl = portalServerUrl + "/pay/passNotShow?tokenName=" + tokenName + "&token=" + token + "&id=" + thanks.getId();
            content += "审核通过但不展示捐赠数据：<br><a href='" + notShowUrl + "'>" + notShowUrl + "</a><br><br>";

            String editUrl = portalServerUrl + "/pay-edit?tokenName=" + tokenName + "&token=" + token + "&id=" + thanks.getId();
            content += "编辑捐赠数据：<br><a href='" + editUrl + "'>" + editUrl + "</a><br><br>";
            System.out.println(editUrl);

            String delNotNotify = portalServerUrl + "/pay/delNotNotify?tokenName=" + tokenName + "&token=" + token + "&id=" + thanks.getId();
            content += "删除该捐赠数据不发送通知邮件：<br><a href='" + delNotNotify + "'>" + delNotNotify + "</a><br><br>";

            String delUrl = portalServerUrl + "/pay/del?tokenName=" + tokenName + "&token=" + token + "&id=" + thanks.getId();
            content += "删除该捐赠数据：<br><a href='" + delUrl + "'>" + delUrl + "</a><br><br>";
            // 邮件内容，第二个参数指定发送的是HTML格式
            helper.setText(content, true);
            sender.send(message);
        } catch (Exception e) {
            log.error("邮件发送失败", e);
        }
        log.info("用户：" + thanks.getUsername() + " 支付:" + thanks.getMoney() + "元 待审核的邮件发送成功");
    }

    @Async
    public void sendEmailPayResult(String sendTo, String title, String content) {

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = null;
        //设置邮件内容
        try {
            helper = new MimeMessageHelper(message, true, "utf-8");

            helper.setTo(sendTo);
            helper.setFrom(mailUserName);
            helper.setSubject(title);
            // true表示HTML格式的邮件
            helper.setText(content, true);

        } catch (MessagingException e) {
            e.printStackTrace();
        }

        // 发送邮件
        sender.send(message);

        log.info("给 " + sendTo + " 的审核结果邮件发送成功");
    }

    /**
     * 验证邮箱
     *
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}
